package com.douya.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 说明：
 *
 * @Author 胡晨烨
 * @Date 2021/8/27
 */
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    public static <T> Map<String, T> getBeanType(Class<T> type) {
        return applicationContext.getBeansOfType(type);
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
